from datetime import datetime
from app import db

# 收货地址模型
class Address(db.Model):
    __tablename__ = 'tb_address'
    
    address_id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='地址ID(主键)')
    user_id = db.Column(db.BigInteger, db.ForeignKey('tb_user.user_id', ondelete='CASCADE'), nullable=False, comment='用户ID')
    receiver = db.Column(db.String(50), nullable=False, comment='收货人')
    phone = db.Column(db.String(20), nullable=False, comment='联系电话')
    province = db.Column(db.String(20), comment='省份')
    city = db.Column(db.String(20), comment='城市')
    district = db.Column(db.String(20), comment='区/县')
    detail = db.Column(db.String(255), nullable=False, comment='详细地址')
    is_default = db.Column(db.Boolean, default=False, comment='是否默认地址(0-否,1-是)')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    
    # 关联 - 使用back_populates与User类匹配
    user = db.relationship('User', back_populates='addresses')
    
    def __repr__(self):
        return f'<Address {self.receiver}>' 